package com.elite.groovy.callback

import com.alibaba.fastjson.JSONArray
import net.sf.json.JSONObject;
import com.elite.groovy.kh.KHDBConfig
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import com.wisdge.dataservice.Result
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: 同步接口数据
 *
 */

public class apiChatPushNotify implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiChatPushNotify.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        long startTime = System.currentTimeMillis();

        String getMethod = request.getMethod();
        log.debug("apiSyncDBData getMethod:" + getMethod);
        String outData = "";
        if (getMethod != "GET") {
            outData = "HTTP请求调用类型错误，接口调用失败：" + getMethod;
            log.error(outData);
            return JSONObject.toJSONString(new Result(Result.ERROR, outData));
        }

        //获取所有参数名
        Enumeration ep = request.getParameterNames();
        while (ep.hasMoreElements()) {
            String parameterName = (String) ep.nextElement();
            log.debug(parameterName + ": " + request.getParameter(parameterName));
        }
         return PushCustomerNotify(eliteBean,"LGLX_PushChatNotify");

    }

    //推送数据
    public static String PushCustomerNotify(EliteBean eliteBean,String APITYPE) {


            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());



            //检查推送数据
            String PustSQL='''select dc.userId,ifnull(CustomerNotify,0) as unReadCount,1 as type 
                                                                from Dg_Chat_Inparams	dc
                                                                left join 
                                                                (
                                                                select userId as userId,max(create_time) as maxtime
                                                                                            from Dg_Chat_Inparams 
                                                                                            where PERM_FLAG=1 
                                                                                            group by userId
                                                                  ) A on A.userId=dc.userId and dc.create_time=A.maxtime
                                                                where A.userId is not null'''

            List<Map<String, Object>> PustSqlList = jt.queryForList(PustSQL);

                    // 获取任务配置基础数据
                    String callURL = KHDBConfig.getAPIURL(eliteBean, APITYPE);
                    log.debug("接口地址:" + callURL);
                    ObjectMapper objectMapper = new ObjectMapper();
                    String jsonOutput = objectMapper.writeValueAsString(PustSqlList);
                    log.debug("推送客户jsonOutput:" + jsonOutput);
        // 开始准备发送
        Map<String, Object> headMap = new HashMap<>()
        headMap.put("Content-Type", "application/json;charset=utf-8;")
                    JSONObject callBody = new JSONObject();
                    callBody.put("list", jsonOutput);
                    String jsonbody = callBody.toString();
                    log.debug("推送客户jsonbody:" + jsonbody);

                    String callRes = Util.doHttpPostUseHead(callURL, jsonbody, "UTF-8", 5000, 0,headMap);
                    log.debug("推送结果："+callRes.toString());
                    return callRes.toString();
                }

            }




